package com.example.sqlitedemo;

import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;

import androidx.annotation.Nullable;

// 自定义一个数据库帮助类
class MyDBHelper extends SQLiteOpenHelper {

    public static final String TEST_TABLE = "tb_test";

    public MyDBHelper(@Nullable Context context, @Nullable String name, @Nullable SQLiteDatabase.CursorFactory factory, int version) {
        super(context, name, factory, version);
    }

    // 只在创建数据库时
    @Override
    public void onCreate(SQLiteDatabase db) {
        // 创建表
        db.execSQL("CREATE TABLE " + TEST_TABLE + "(" +
                "id integer primary key autoincrement," +  // 从1开始自增
                "name varchar(50) unique," +
                "phone varchar(13))");
    }

    // 数据库版本更新时
    @Override
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {

    }
}
